/*
 * @Author: zhangyang
 * @Date: 2020-12-03 14:06:59
 * @LastEditTime: 2022-02-24 16:15:47
 * @Description: 前端路由
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
//  路由配置
import pages from './pages.json';
// 通用路由，无权限
import commonRoute from './common/index';

declare module 'vue-router' {
  interface RouteMeta {
    /**
     * 标题
     */
    title: string;
    /**
     * 小图标
     */
    icon?: string;
    /**
     * 是否固定
     */
    affix?: boolean;
    /**
     * 是否禁用缓存
     */
    noCache?: boolean;
  }
}
const routes: RouteRecordRaw[] = [
  ...commonRoute,
  // 进入后台之后的默认界面
  {
    path: '/',
    redirect: '/welcome',
  },
];
/**
 * 根据约定的配置文件生成路由
 */
for (const path of Object.keys(pages)) {
  const _conf = (pages as unknown as any)[path] as YoungRouteItem[];
  const routeItem: RouteRecordRaw = {
    path: `/${path}`,
    component: () => import('/src/layout/index.vue'),
    redirect: `/${path}/${_conf[0].name}`,
    children: [],
  };
  for (const { name, title } of _conf) {
    routeItem.children?.push({
      path: `/${path}/${name}`,
      name,
      // 动态生成的导入名称必须为相对路径，而且必须有扩展名
      // https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations
      component: () => import(`../views/${path}/${name}.vue`),
      meta: { title },
    });
  }
  routes.push(routeItem);
}
// 页面不存在，404,必须放在最后！！！
routes.push({
  path: '/:pathMatch(.*)*',
  name: 'NotFound',
  component: () => import('/src/views/404.vue'),
});
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    return { top: 0 };
  },
});

export default router;
export { routes };
